package com.hnjmxy.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.Customer;
import service.CustomerService;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		//获取用户输入的数据
		response.setCharacterEncoding("utf-8");
		String name=request.getParameter("c_name");
		String pwd=request.getParameter("c_pass");
		String code=request.getParameter("c_code");
		HttpSession session1 =request.getSession();
		String c_code=(String)session1.getAttribute("c_code");
		Customer c=new Customer();
		c.setC_name(name);
		c.setC_pass(pwd);
		//编写业务逻辑
		CustomerService cservice=new CustomerService();
		Customer cu=cservice.loginByName(c);
		//响应
		if(cu!=null&code.equalsIgnoreCase(c_code)) {
			HttpSession session=request.getSession(true);
			session.setAttribute("c",cu);
			response.sendRedirect("assess.jsp");
		}else {
			response.sendRedirect("login.jsp");
		}
	}


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
